//
//  OMOrderBaseTableCell.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/3.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderBaseTableCell.h"

#import "OMOrderMoreView.h"
#import <YYText.h>
#import "OMOrderButtonActionHelper.h"
@interface OMOrderBaseTableCell()
@property (nonatomic, strong) OMOrderMoreView *moreView;
@end

@implementation OMOrderBaseTableCell

- (void)configure
{
    [super configure];
    [self.contentView addSubview:self.statusView];
    [self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@(SM_SIZE(0)));
        make.height.equalTo(@(OMOrderStatusViewHeight));
    }];

    [self.statusView addSubview:self.statusLabel];
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-25)));
        make.centerY.equalTo(self.statusView.mas_centerY);
    }];

    [self.statusView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(25)));
        make.centerY.equalTo(self.statusView.mas_centerY);
    }];

    [self.contentView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.statusView.mas_bottom).offset(SM_SIZE(0));
        make.height.equalTo(@0);
    }];

    [self.contentView addSubview:self.moreView];
    [self.moreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.tableView.mas_bottom);
        make.height.equalTo(@(OMOrderMoreViewHeight));
    }];


    [self.contentView addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(SM_SIZE(0)));
        make.height.equalTo(@(OMOrderBottomViewHeight));
        make.top.equalTo(self.moreView.mas_bottom);
    }];
    [self.bottomView addSubview:self.infoLabel];
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-25)));
        make.top.equalTo(@(SM_SIZE(20)));
    }];
}


#pragma mark - Getters

- (OMOrderMoreView *)moreView
{
    if (!_moreView) {
        _moreView = [OMOrderMoreView new];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
        @weakify(self);
        [[tap rac_gestureSignal]subscribeNext:^(id x) {
            @strongify(self);
            NaviPush(@"OMOrderDetailTabsViewController", (@{@"orderSn":self.order.sn,@"selectedIndex":@(1)}));
        }];
        _moreView.userInteractionEnabled = true;
        [_moreView addGestureRecognizer:tap];
    }
    return _moreView;
}

- (UIView *)statusView
{
    if (!_statusView) {
        _statusView = [UIView new];
//        _statusView.backgroundColor = SM_dotRedColor;
    }
    return _statusView;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = SM_NFont(28);
        _timeLabel.text = @"2017-05-05 17:55";
    }
    return _timeLabel;
}

- (UILabel *)infoLabel
{
    if (!_infoLabel) {
        _infoLabel = [UILabel new];
        _infoLabel.font = SM_CFont(28);
        _infoLabel.text = @"共23件商品 应付：￥107";
    }
    return _infoLabel;
}

- (UILabel *)statusLabel
{
    if (!_statusLabel) {
        _statusLabel = [UILabel new];
        _statusLabel.font = SM_CFont(28);
        _statusLabel.text = @"待付款";
        _statusLabel.textColor = HEXCOLOR(@"#FE810D");
    }
    return _statusLabel;
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = SM_whiteColor;
        _tableView.separatorColor = SM_naviShadowColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = DBL_EPSILON;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, DBL_EPSILON)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorColor = SM_naviShadowColor;
        _tableView.scrollEnabled = false;
    }
    return _tableView;
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
    }
    return _bottomView;
}

- (UIButton*)createButtonWithTitle:(NSString*)title
                         andIsFill:(BOOL)isFull
{
    UIButton *button = [UIButton new];
    [button setTitleColor:SM_blackColor forState:UIControlStateNormal];
    [button.titleLabel setFont:SM_CFont(31)];
    button.layer.borderWidth = 1.0f;
    button.layer.cornerRadius = SM_SIZE(5);
    button.clipsToBounds = true;
    [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self configureButton:button withTitle:title andIsFill:isFull];
    return button;
}

- (void)configureButton:(UIButton *)button
              withTitle:(NSString *)title
              andIsFill:(BOOL)isFull
{
    [button setTitle:title forState:UIControlStateNormal];
    if (isFull) {
        [button setBackgroundImage:[UIImage imageWithColor:SM_sangouColor] forState:UIControlStateNormal];
    }else {
        [button setBackgroundImage:[UIImage imageWithColor:SM_whiteColor] forState:UIControlStateNormal];
        button.layer.borderColor = SM_blackColor.CGColor;
    }
}

#define kOMOrderItemSeparatorLineMarginLeft SM_SIZE(130)
-(void)layoutSubviews
{
    [super layoutSubviews];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,kOMOrderItemSeparatorLineMarginLeft,0,0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,kOMOrderItemSeparatorLineMarginLeft,0,0)];
    }
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,kOMOrderItemSeparatorLineMarginLeft,0,0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsMake(0,kOMOrderItemSeparatorLineMarginLeft,0,0)];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return MIN(2,self.order.orderItems.count);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMOrderSubmitProductTableCell *cell = [OMOrderSubmitProductTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:self.order.orderItems[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NaviPush(@"OMOrderDetailTabsViewController", (@{@"orderSn":self.order.sn,@"selectedIndex":@(1)}));
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return OMOrderSubCellHeight;
}


+ (NSString *)orderStatusStr:(NSUInteger)status
{
    switch (status) {
        case 0:return @"待支付";
        case 1:return @"待审核";
        case 2:return @"待发货";
        case 3:return @"配送中";
        case 4:return @"待评价";
        case 5:return @"已评价";
        case 6:return @"交易失败";
        case 7:return @"已取消";
        case 8:return @"已拒绝";
        default:return @"未完成";
    }


}



- (void)configureCellWithObj:(OMOrder *)order
{
    _order = order;
    //时间
    self.timeLabel.text = order.modifyDate;
    //状态
    self.statusLabel.text = self.order.statusName;

    //信息栏
    NSString *info = [NSString stringWithFormat:@"共 %lu 件商品 %@付：￥%.2f",(unsigned long)self.order.quantity,self.order.status?@"实":@"应",self.order.amount.floatValue];
    if (self.order.status == 0) {//待支付
        NSMutableAttributedString *infos = [[NSMutableAttributedString alloc]initWithString:info];
        NSRange rangeOfNum = [info rangeOfString:[NSString stringWithFormat:@"%lu 件",self.order.quantity]];
        [infos yy_setFont:SM_NFont(28) range:NSMakeRange(rangeOfNum.location, rangeOfNum.length-2)];
        [infos yy_setColor:SM_redColor range:NSMakeRange(rangeOfNum.location, rangeOfNum.length-2)];
        NSRange rangeOfPrice = [info rangeOfString:[NSString stringWithFormat:@"￥%.2f",self.order.amount.floatValue]];
        [infos yy_setFont:SM_NFont(28) range:rangeOfPrice];
        [infos yy_setColor:SM_redColor range:rangeOfPrice];
        self.infoLabel.attributedText = infos;
    }else {
        self.infoLabel.text = info;
    }

    //显示查看更多商品
    [self.moreView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(self.order.orderItems.count>2?OMOrderMoreViewHeight:0));
    }];

    //商品列表
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(OBT_MIN_TABLE_HEIGHT(OMOrderSubCellHeight*order.orderItems.count)));
    }];

    [self.tableView reloadData];

}


- (void)buttonTap:(UIButton *)sender
{
    [OMOrderButtonActionHelper actionWithButton:sender Order:self.order];
}


+ (CGFloat)cellHeightWithOrder:(OMOrder*)order
{
    return OBT_MIN_TABLE_HEIGHT((OMOrderSubCellHeight+1)*order.orderItems.count)+OMOrderBottomViewHeight+OMOrderStatusViewHeight+(order.orderItems.count>2?OMOrderMoreViewHeight:0)-([OMOrderButtonAdapter buttonTitlesWithStatus:order.status showInList:YES].count?0:SM_SIZE(60));
}
@end
